package javafx.webview.demo.service;

import com.alibaba.fastjson.JSON;
import netscape.javascript.JSObject;

import java.util.Arrays;
import java.util.List;

import static java.lang.Thread.sleep;
import static java.util.Arrays.asList;
import static java.util.Collections.shuffle;
import static javafx.application.Platform.runLater;
import static javafx.webview.demo.utils.Java2JavascriptUtils.call;

public class FruitsService {

	public void loadHero(final Object callBack){
        List<String> strings = Arrays.asList("寒冰", "德码", "劫");
        String s = JSON.toJSONString(strings);
        ((JSObject)callBack).eval("this("+JSON.toJSONString(strings)+")");
        //((JSObject)callBack).call("this",s);

    }

	//模拟成为异步方法
	public void loadFruits(final Object callbackfunction){
		
		//模拟数据库
		final List<String> fruits = asList(
				new String[] { "orange", "apple", "banana", "strawberry" });
		
		//开启一个异步线程
		new Thread( () -> {
				try {
					shuffle(fruits);
					sleep(1000); //模拟网络请求时间的过程
					runLater( () -> 							
						call(callbackfunction, JSON.toJSONString(fruits))
					);
				} catch (InterruptedException e) {	}
			}
		).start();
	}
}
